﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: This class manages all aspects of encryption.  This will house all the used encryption plugins and handle decryption
\**************************************************************************************************/

import arkadium.EncryptionPlugin;
import arkadium.TEAEncryption;
import arkadium.MD5Encryption;
import mx.utils.Delegate;
import arkadium.EventCenter;


class arkadium.EncryptionManager {
	private var _aEncryptionPlugins:Array;// stores all GUIPanels that have been registered.
	public var _sEcryptionType:String;
	public var _bIsEncrypted:Boolean;
	public var _sKey:String = "sjri5jf85jdor560";

	public function EncryptionManager() {
		// Since this class is a singleton and is linked to a movieclip contained on stage we can set 
		// ourselves to this instance since the constructor is called automatically by flash.

		_aEncryptionPlugins = new Array();
	}
	/**************************************************************************************************\
	onInitialize - Called by main to initialize anything that couldn't be handled by the constructor.
	\**************************************************************************************************/
	public function onInitialize(aEPluginsToInitialize:Array, sDefaultEncryption:String) {
		_sEcryptionType = sDefaultEncryption;

		// Let's initialize all registered Encryption Types
		for (var i:Number = 0; i<aEPluginsToInitialize.length; ++i) {
			if (aEPluginsToInitialize[i] == "TEA") {
				_aEncryptionPlugins[i] = new TEAEncryption();
			} else if (aEPluginsToInitialize[i] == "MD5") {
				_aEncryptionPlugins[i] = new MD5Encryption();
			}
			_aEncryptionPlugins[i].onInitialize();
		}
	}
	public function decryptParams(oDecryptionObject_obj, sParams:String) {

		getEPluginInstance(_sEcryptionType).decryptAndParse(oDecryptionObject_obj,sParams,_sKey);

		//trace("going to decrypt "+sParams+" with the encryption:"+_sEcryptionType);

	}
	public function encryptParams(dParams:String):String {

		//trace(getEPluginInstance(_sEcryptionType).encrypt(dParams, _sKey));
		return (getEPluginInstance(_sEcryptionType).encrypt(dParams, _sKey));


	}

	/**************************************************************************************************\
	getPanelInstance - This function is used internally to find and return registered GUI Panels.
	\**************************************************************************************************/
	private function getEPluginInstance(sEPluginName:String) {
		for (var i:Number = 0; i<_aEncryptionPlugins.length; ++i) {
			if ((sEPluginName+"_ENCRYPTION") == _aEncryptionPlugins[i].getEPluginName()) {
				return _aEncryptionPlugins[i];
			}
		}
		return null;
	}


}